package priv.zgh.forum.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import priv.zgh.forum.auth.entity.Power;
import priv.zgh.forum.auth.entity.Role;
import priv.zgh.forum.auth.entity.RolePower;
import priv.zgh.forum.auth.service.PowerService;
import priv.zgh.forum.auth.service.RolePowerService;
import priv.zgh.forum.auth.service.RoleService;
import priv.zgh.forum.auth.vo.RolePowerVo;
import priv.zgh.forum.utils.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZGH
 * @since 2021-10-22
 */
@CrossOrigin
@RestController
@RequestMapping("/auth/role-power")
public class RolePowerController {
    RoleService roleService;
    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    PowerService powerService;
    @Autowired
    public void setPowerService(PowerService powerService) {
        this.powerService = powerService;
    }

    RolePowerService rolePowerService;
    @Autowired
    public void setRolePowerService(RolePowerService rolePowerService) {
        this.rolePowerService = rolePowerService;
    }

    @ApiOperation(value = "根据角色ID来获取角色与角色的权限信息.")
    @GetMapping(path = "/getRolePower/{id}")
    public R getRolePowerById(@PathVariable String id){
        return R.ok().data("rolePowerVo",rolePowerService.getRolePowerById(id));
    }

    @ApiOperation(value = "根据角色ID来获取角色不拥有的权限信息.")
    @GetMapping(path = "/getRoleNotPower/{id}")
    public R getRoleNotPower(@PathVariable String id){
        return R.ok().data("powers",rolePowerService.getRoleNotPower(id));
    }

    @ApiOperation(value = "更新此角色权限信息")
    @PostMapping(path = "/updateRolePower")
    public R getRoleNotPower(@RequestBody RolePowerVo rolePowerVo){
        // 更新角色信息
        Role role = new Role();
        role.setId(rolePowerVo.getRole().getId());
        role.setRoleName(rolePowerVo.getRole().getRoleName());
        roleService.updateById(role);
        // TODO 能否重写算法提高性能?
        // 首先与属于该角色的权限比较删除
        List<Power> possessPowers = rolePowerService.getRolePowerById(rolePowerVo.getRole().getId()).getPowers();
        for(Power item : possessPowers){
            boolean flag = false;
            for(Power x : rolePowerVo.getPowers()){
                // 遍历,没有的就删除原来拥有的权限
                if(x.getId() == item.getId()){
                    flag = true;
                    break;
                }
            }
            if(!flag){
                QueryWrapper<RolePower> rolePowerQueryWrapper = new QueryWrapper<>();
                rolePowerQueryWrapper.eq("role_id",rolePowerVo.getRole().getId()).and(wrapper -> wrapper.eq("power_id",item.getId()));
                rolePowerService.remove(rolePowerQueryWrapper);
            }
        }
        // 之后与不属于该角色的权限对比
        List<Power> notPowers = rolePowerService.getRoleNotPower(rolePowerVo.getRole().getId());
        for(Power item : notPowers){
            for(Power x : rolePowerVo.getPowers()){
                // 遍历,碰到相同的就添加
                if(x.getId().equals(item.getId())){
                    RolePower rolePower = new RolePower();
                    rolePower.setRoleId(rolePowerVo.getRole().getId());
                    rolePower.setPowerId(x.getId());
                    rolePowerService.save(rolePower);
                    break;
                }
            }
        }
        return R.ok();
    }

    @ApiOperation(value = "保存此角色权限信息")
    @PostMapping(path = "/saveRolePower")
    public R saveRolePower(@RequestBody RolePowerVo rolePowerVo){
        // 先保存角色信息
        Role role = new Role();
        role.setRoleName(rolePowerVo.getRole().getRoleName());
        roleService.save(role);
        // 保存角色与权限信息
        for(Power item : rolePowerVo.getPowers()){
            RolePower rolePower = new RolePower();
            rolePower.setRoleId(role.getId());
            rolePower.setPowerId(item.getId());
            rolePowerService.save(rolePower);
        }
        return R.ok();
    }
}

